package com.bookmarket.controller;

import com.bookmarket.entity.Btype;
import com.bookmarket.entity.Stype;
import com.bookmarket.service.IClassifyService;
import com.github.pagehelper.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cf")
@SessionAttributes(names="list")
public class LJClassifyController {

    @Resource
    private IClassifyService iClassifyService;
    //查(json传数据;通过视图跳转必须json格式)
    @RequestMapping(value = "/findBtypeAll")
    public ModelAndView findBtypeAll(){
        ModelAndView mv=new ModelAndView();
        List<Btype> list=iClassifyService.selectBtype();
        mv.addObject("list",list);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }
    //增
    @RequestMapping("/zeng")
    public ModelAndView zeng(Btype btype){
        ModelAndView mv=new ModelAndView();
        iClassifyService.insertBtype(btype);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }
    //删
    @RequestMapping("/del")
    public ModelAndView del(int id){
        ModelAndView mv=new ModelAndView();
        iClassifyService.delBtype(id);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }
    //改
    @RequestMapping("/gai")
    public ModelAndView gai(Btype btype){
        ModelAndView mv=new ModelAndView();
        iClassifyService.updateBtype(btype);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }
    //分页语句查(取代查询全部;包含模糊搜索)
    @RequestMapping("/queryByPage")
    public Map queryByPage(int pageNum, int pageSize,Btype btype) {
        Page page =iClassifyService.queryB(pageNum,pageSize, btype);
        Map map = new HashMap();
        map.put("page", page);
        map.put("totalPage", page.getPages());
        return map;
    }
    /**********************************************************************************************/
    //增
    @RequestMapping("/jia")
    public ModelAndView jia(Stype stype,String btype_id){
        ModelAndView mv=new ModelAndView();
        Btype b=iClassifyService.selectById(btype_id);
        if (b==null){
            mv.setView(new MappingJackson2JsonView());
            return mv;
        }else{
            stype.setBtype(b);
            iClassifyService.insertStype(stype);
            mv.setView(new MappingJackson2JsonView());
            return mv;
        }
    }
    //删
    @RequestMapping("/shan")
    public ModelAndView shan(int id){
        ModelAndView mv=new ModelAndView();
        iClassifyService.delStype(id);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }
    //改
    @RequestMapping("/upda")
    public ModelAndView upda(Stype stype){
        ModelAndView mv=new ModelAndView();
        iClassifyService.updateStype(stype);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }
    //分页查询
    @RequestMapping("/queryPage")
    public Map queryPage(int pageNum, int pageSize,Stype stype) {
        Page page =iClassifyService.queryS(pageNum,pageSize,stype);
        Map map = new HashMap();
        map.put("page", page);
        map.put("totalPage", page.getPages());
        return map;
    }
    public IClassifyService getiClassifyService() {
        return iClassifyService;
    }

    public void setiClassifyService(IClassifyService iClassifyService) {
        this.iClassifyService = iClassifyService;
    }
}
